
function bindone(){
  var i;
  var k;
  var app = getApp();
  i = Math.floor(Math.random() * 2);
  if(i == 0 ){//加法
    app.globalData.addend1 = Math.floor(Math.random() * 100);
    k = 100 - app.globalData.addend1;
    app.globalData.addend2 = Math.floor(Math.random() * k);
    app.globalData.answer = (app.globalData.addend1 + app.globalData.addend2);
    app.globalData.question = (app.globalData.addend1 + " + " + app.globalData.addend2 + " = ");
  }
  if(i == 1){//减法
    app.globalData.addend1 = Math.floor(Math.random() * 100);
    app.globalData.addend2 = Math.floor(Math.random() * 100);
    app.globalData.answer = Math.abs(app.globalData.addend1 - app.globalData.addend2);
    if(app.globalData.addend1 >= app.globalData.addend2){
      app.globalData.question = (app.globalData.addend1 + " - " + app.globalData.addend2 + " = ");
    }
    else{
      app.globalData.question = (app.globalData.addend2 + " - " + app.globalData.addend1 + " = ");
    }
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: "无",
    data_input:{},
    cir:"",
    r_n:0,
    a_n:0,
    answer:0,
    remainder : 0,
    modalHidden:true,
    time:20,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  onShow: function(){
    if(this.data.time=="测试结束"||this.data.num=="无"){
      this.setData({
        num:20,
        time:90,
        r_n:0,
        a_n:0,
      })
      this.Numdown()
    }
    wx.setNavigationBarTitle({
      title: '测验 一年级'
    })
    bindone();
    this.setData({
      answer:getApp().globalData.answer,
      cir:getApp().globalData.question,
      remainder:getApp().globalData.remainder,
    });
  },
  onUnload:function(){
    if(this.data.num != "无") {
      this.setData({
        num:0,
      })
      this.endNum();
    }
  },
  Numdown: function() {
    var that = this;
    var time = that.data.time;
    setTimeout(function() {
        that.setData({
          time:time-1
        })
        that.endNum();
    }, 1000)
  },
  endNum: function() {
    var that = this,
    num = that.data.num;
    var time = that.data.time;
    if (num == 0||time == 0) {
      that.setData({
        time: "测试结束",
        num: "无"
      })
      var str;
      if(this.data.a_n == 0) str = "你没有完成任何试题"
      else str = (this.data.r_n/this.data.a_n).toFixed(2);
      wx.showModal({
        title: '测试结束',
        content:'完成题目：'+this.data.a_n+'\n'+'正确题目：'+this.data.r_n+'\n'+'正确率：'+str,
        success: function(res) {
          if(res.cancel){
          //这个跳转是左边按钮的跳转链接
            wx.switchTab({
              url: '../test/test'
            })
          }else{
          //这里是右边按钮的跳转链接
            wx.switchTab({
              url: '../test/test'
            })
          }
        }
      })
    } 
    else{
      if(this.data.num == "无"){
        var that = this;
        that.setData({
          time: "测试结束"
        })
      }
      else{
        var that = this;
        that.Numdown();
      }
    }
  },

  datain: function(e){
    this.setData({
      data_input:e.detail.value,
    });
  },

  submit:function(){
    this.data.num--;
    try{
      getApp().data.yop = wx.getStorageSync('wrong_n')
    }catch(e){
      getApp().data.yop = 0;
    }
    getApp().data.yop++,
    this.data.a_n++;
    wx.setStorage({
      data: getApp().data.yop,
      key: "wrong_n",
    })
    var num=(getApp().data.yop-1) % 30;
    try{
      getApp().data.data1 = wx.getStorageSync('his')
      if(!getApp().data.data1) getApp().data.data1 = [];
    }catch(e){
    }
    getApp().data.data1[num]=this.data.cir+this.data.answer;
    wx.setStorage({
      data: getApp().data.data1,
      key: "his",
    })
    if(this.data.answer==this.data.data_input){
      this.data.r_n++;
    } else{
      try{
        getApp().data.xop = wx.getStorageSync('right_n')
      }catch(e){
      }
      getApp().data.xop++;
      wx.setStorage({
        data: getApp().data.xop,
        key: "right_n",
      })
      var num=(getApp().data.xop-1) % 30;
      try{
        getApp().data.data2 = wx.getStorageSync('wro')
        if(!getApp().data.data2) getApp().data.data2 = [];
      }catch(e){
      }
      getApp().data.data2[num]=this.data.cir+this.data.answer;
      wx.setStorage({
        data: getApp().data.data2,
        key: "wro",
      })
      this.setData({
        modalHidden:!this.data.modalHidden
      })
    }
    var that=this;
    if(this.data.num == 0) that.endNum();
    else that.onShow();
  },
})